﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.produceDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.produceLinq;
using dhDataAcess.produceDAL;
using dhDataAcess.sysDAL;

namespace dhSystemBLL.produceBLL
{
  public  class orderStockReturnBLL
    {
      private orderStockReturnMasterTableAdapter _orderStockReturnMasterlAdapter = null;
      private orderStockReturnMasterTableAdapter orderStockReturnMasterAdapter
        {
            get
            {
                if (_orderStockReturnMasterlAdapter == null) _orderStockReturnMasterlAdapter = new orderStockReturnMasterTableAdapter();
                return _orderStockReturnMasterlAdapter;

            }
        }
      private orderStockReturnDetailTableAdapter _orderStockReturnDetailAdapter = null;
      private orderStockReturnDetailTableAdapter orderStockReturnDetailAdapter
        {
            get
            {
                if (_orderStockReturnDetailAdapter == null) _orderStockReturnDetailAdapter = new orderStockReturnDetailTableAdapter();
                return _orderStockReturnDetailAdapter;

            }
        }
        /// <summary>
      /// 查询订单成品退库主表
        /// </summary>
        /// <returns></returns>

      public produceDataSet.orderStockReturnMasterDataTable getorderStockOutMasterAll()
        {
            try
            {
                return orderStockReturnMasterAdapter.GetDataorderStockReturnMasterALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
      /// 查询订单成品退库从表
        /// </summary>
        /// <returns></returns>

      public produceDataSet.orderStockReturnDetailDataTable getorderStockReturnDetailAll()
        {
            try
            {
                return orderStockReturnDetailAdapter.GetDataorderStockReturnDetailALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
      /// 根据入库单号查询订单成品退库表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
      public produceDataSet.orderStockReturnMasterDataTable getorderStockReturnMasterAllBybillNo(string billNo)
        {
            try
            {
                return orderStockReturnMasterAdapter.GetDataorderStockReturnMasterBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
      /// 根据入库单号查询订单成品退库从表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
      public produceDataSet.orderStockReturnDetailDataTable getsorderStockReturnDetailAllBybillNo(string billNo)
        {
            try
            {
                return orderStockReturnDetailAdapter.GetDataorderStockReturnDetailBybillNo (billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
      /// 修改订单成品退库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
      public int orderStockReturnInsert(orderStockReturnMasterModel row, produceDataSet.orderStockReturnDetailDataTable pTable, string state, string billNo, string Date)
        {
            orderStockReturnDetailModel orderStockReturnDetailRow = new orderStockReturnDetailModel();
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                orderStockReturnMasterDAL orderStockReturnMaster = new orderStockReturnMasterDAL(tn, conn);
                orderStockReturnDetailDAL orderStockReturnDetail = new orderStockReturnDetailDAL(tn, conn);
                basListDAL basList = new basListDAL(tn, conn);
                if (state == "新增")
                {

                    produceDataSet.orderStockReturnMasterDataTable orderStockOutTable = getorderStockReturnMasterAllBybillNo(billNo);
                    if (orderStockOutTable.Rows.Count == 0)
                    {
                        orderStockReturnMaster.orderStockReturnMasterInsert(row);
                    }
                    else
                    {
                        row.billNo = "R" + Date + "-" + basicSql.getOrderSerail("订单成品退库单号");
                        orderStockReturnMaster.orderStockReturnMasterInsert(row);
                    }

                    //更新单据编号的过程 
                    string orderSerial = basicSql.getOrderSerail("订单成品退库单号");
                    basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "订单成品退库单号");
                }
                if (state == "编辑")
                {
                    orderStockReturnMaster.orderStockReturnMasterUpdate(row);
                }


                foreach (produceDataSet.orderStockReturnDetailRow detailRow in pTable)
                {
                    if (detailRow.RowState != DataRowState.Deleted)
                    {
                        orderStockReturnDetailRow.billNo = detailRow.billNo;
                        orderStockReturnDetailRow.orderDetailNo = detailRow.orderDetailNo;
                        orderStockReturnDetailRow.pName = detailRow.pName;
                        orderStockReturnDetailRow.qty = detailRow.qty;
                        orderStockReturnDetailRow.unit = detailRow.unit;
                        orderStockReturnDetailRow.boxno = detailRow.boxno;
                        orderStockReturnDetailRow.boxList = detailRow.boxList;
                        orderStockReturnDetailRow.weightJ = detailRow.weightJ;
                        orderStockReturnDetailRow.weightM = detailRow.weightM;
                        orderStockReturnDetailRow.createDate = DateTime.Now;

                        if (!detailRow.IsmarkNull())
                        {
                            orderStockReturnDetailRow.mark = orderStockReturnDetailRow.mark;
                        }
                        else
                        {
                            orderStockReturnDetailRow.mark = "";
                        }
                    }

                    //编辑子表记录,新增
                    if (detailRow.RowState == DataRowState.Added)
                    {

                        orderStockReturnDetail.orderStockReturnDetailInsert(orderStockReturnDetailRow);


                    }
                    //修改
                    if (detailRow.RowState == DataRowState.Modified)
                    {
                        orderStockReturnDetail.orderStockReturnDetailUpdate(orderStockReturnDetailRow);
                    }
                    //删除
                    if (detailRow.RowState == DataRowState.Deleted)
                    {
                        DataRow delrow = (DataRow)detailRow;
                        orderStockReturnDetail.orderStockReturnDetailDel(delrow["recordid", DataRowVersion.Original].ToString());
                    }


                }

                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个入库单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }


        /// 删除一行订单成品退库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
      public int orderStockReturnMasterDel(string billNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                orderStockReturnMasterDAL orderStockReturnMaster = new orderStockReturnMasterDAL(tn, conn);
                orderStockReturnMaster.orderStockReturnMasterDel(billNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
    }
}
